/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddPostMortem.java
 *
 * Created on Sep 8, 2011, 2:07:15 AM
 */
package Views;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import Models.PostMortem;
import Models.Cases;
import Controllers.ManagingPosMortemDAO;
import Controllers.CaseHistoryDAO;
/**
 *
 * @author TranDangThuan
 */
public class AddPostMortem extends javax.swing.JPanel {

    private DefaultTableModel tblModel;
    private List<Integer> caseIDList = new ArrayList<Integer>();
    
    /** Creates new form AddPostMortem */
    public AddPostMortem() {
        initComponents();
        
        tblModel = new DefaultTableModel();
        tblModel.addColumn("Mortality ID");
        tblModel.addColumn("Mortality Name");
        tblModel.addColumn("Mortality Time");
        tblModel.addColumn("Case ID");
        tblModel.addColumn("Check In Date");
        tblModel.addColumn("Cause Of Death");
        tblModel.addColumn("Doctor Name");
        tblPosmortem.setModel(tblModel);
        loadDataCombobox();
        loadDataAllTable();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        comboxCaseID = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        spnnMortalityTime = new javax.swing.JSpinner();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        spnnCheckInDate = new javax.swing.JSpinner();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtMortalityName = new javax.swing.JTextArea();
        jScrollPane3 = new javax.swing.JScrollPane();
        txtCauseOfDeath = new javax.swing.JTextArea();
        jScrollPane4 = new javax.swing.JScrollPane();
        txtDoctorName = new javax.swing.JTextArea();
        jPanel2 = new javax.swing.JPanel();
        butEdit = new javax.swing.JButton();
        butSearch = new javax.swing.JButton();
        butAddNew = new javax.swing.JButton();
        butShow = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblPosmortem = new javax.swing.JTable();
        jLabel10 = new javax.swing.JLabel();

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 13));
        jLabel11.setText("Case ID:");

        comboxCaseID.setFont(new java.awt.Font("Tahoma", 0, 13));
        comboxCaseID.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 13));
        jLabel8.setText("Doctor Name:");

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 13));
        jLabel7.setText("Cause Of Death:");

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 13));
        jLabel1.setText("Mortality Name:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 13));
        jLabel2.setText("Mortality Time:");

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 13));
        jLabel5.setText("CheckIn Date:");

        txtMortalityName.setColumns(20);
        txtMortalityName.setRows(5);
        jScrollPane1.setViewportView(txtMortalityName);

        txtCauseOfDeath.setColumns(20);
        txtCauseOfDeath.setRows(5);
        jScrollPane3.setViewportView(txtCauseOfDeath);

        txtDoctorName.setColumns(20);
        txtDoctorName.setRows(5);
        jScrollPane4.setViewportView(txtDoctorName);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jLabel5))
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(spnnCheckInDate)
                    .addComponent(spnnMortalityTime)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addComponent(jLabel7)
                    .addComponent(jLabel11))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(comboxCaseID, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE)))
                .addGap(32, 32, 32))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel7)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(spnnMortalityTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2)
                            .addComponent(jLabel8))
                        .addGap(34, 34, 34)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(spnnCheckInDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5)))
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboxCaseID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addGap(49, 49, 49))
        );

        butEdit.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        butEdit.setText("Edit");

        butSearch.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        butSearch.setText("Search");

        butAddNew.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        butAddNew.setText("Add New");
        butAddNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butAddNewActionPerformed(evt);
            }
        });

        butShow.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        butShow.setText("Show");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(butAddNew, javax.swing.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE)
                    .addComponent(butEdit, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(butSearch, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE)
                    .addComponent(butShow, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addComponent(butAddNew)
                .addGap(18, 18, 18)
                .addComponent(butEdit)
                .addGap(29, 29, 29)
                .addComponent(butSearch)
                .addGap(26, 26, 26)
                .addComponent(butShow)
                .addContainerGap(27, Short.MAX_VALUE))
        );

        tblPosmortem.setBackground(new java.awt.Color(236, 233, 216));
        tblPosmortem.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(tblPosmortem);

        jLabel10.setBackground(new java.awt.Color(51, 0, 204));
        jLabel10.setFont(new java.awt.Font("Times New Roman", 1, 18));
        jLabel10.setText("MANAGING POSMORTEM DETAILS");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 674, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(155, 155, 155)
                        .addComponent(jLabel10)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 540, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(60, 60, 60))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void butAddNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butAddNewActionPerformed
        SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
        if (txtMortalityName.getText().trim().length() == 0) {
            JOptionPane.showConfirmDialog(null, "You must Enter Mortality Name", "Error Message", JOptionPane.WARNING_MESSAGE);
        } else if (txtCauseOfDeath.getText().trim().length() == 0) {
            JOptionPane.showConfirmDialog(null, "You must Enter Cause Of Death", "Error Message", JOptionPane.WARNING_MESSAGE);
        } else if (txtDoctorName.getText().trim().length() == 0) {
            JOptionPane.showConfirmDialog(null, "You must Enter Doctor Name", "Error Message", JOptionPane.WARNING_MESSAGE);
        } else {
            PostMortem postMortem = new PostMortem();
            //set case ID
            postMortem.setCaseID(Integer.parseInt(comboxCaseID.getSelectedItem().toString()));
            //set causeOfDate
            postMortem.setCauseOfDeath(txtCauseOfDeath.getText());
            postMortem.setMortalityName(txtMortalityName.getText());
            postMortem.setDoctorName(txtDoctorName.getText());
            String strCheckInDate = formatter.format(spnnCheckInDate.getValue());
            String strMortalityTime = formatter.format(spnnCheckInDate.getValue());
            try {
                postMortem.setCheckInDate(formatter.parse(strCheckInDate));
                postMortem.setMortalityTime(formatter.parse(strMortalityTime));
            } catch (ParseException ex) {
                ex.printStackTrace();
            }
            if (ManagingPosMortemDAO.addNewPosMortem(postMortem)) {
                JOptionPane.showConfirmDialog(null, "Add New successful", "Error Message", JOptionPane.WARNING_MESSAGE);
            } else {
                JOptionPane.showConfirmDialog(null, "Add New not successful", "Error Message", JOptionPane.WARNING_MESSAGE);
            }
        }
    }//GEN-LAST:event_butAddNewActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton butAddNew;
    private javax.swing.JButton butEdit;
    private javax.swing.JButton butSearch;
    private javax.swing.JButton butShow;
    private javax.swing.JComboBox comboxCaseID;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JSpinner spnnCheckInDate;
    private javax.swing.JSpinner spnnMortalityTime;
    private javax.swing.JTable tblPosmortem;
    private javax.swing.JTextArea txtCauseOfDeath;
    private javax.swing.JTextArea txtDoctorName;
    private javax.swing.JTextArea txtMortalityName;
    // End of variables declaration//GEN-END:variables
    
    private void loadDataCombobox() {
        CaseHistoryDAO caseDAO = new CaseHistoryDAO();
        ArrayList<Cases> listCase = caseDAO.loadALL();
        comboxCaseID.removeAllItems();
        for (Cases cases : listCase) {
            caseIDList.add(cases.getCaseID());
            comboxCaseID.addItem(cases.getCaseID());
        }
    }

    private void loadDataAllTable() {
        List<PostMortem> listPostMortem = ManagingPosMortemDAO.loadAllPostMortem();
        while (tblPosmortem.getRowCount()>0) {            
            tblPosmortem.remove(0);
        }
        for (PostMortem postMortem : listPostMortem) {
            tblModel.addRow(new String[] {String.valueOf(postMortem.getCaseID()), postMortem.getMortalityName(),postMortem.getMortalityTime().toString()});
        }
    }
}
